package com.linghu.controller;

import com.aliyun.broadscope.bailian.sdk.AccessTokenClient;
import com.aliyun.broadscope.bailian.sdk.ApplicationClient;
import com.aliyun.broadscope.bailian.sdk.models.BaiLianConfig;
import com.linghu.entity.cont.Constrant;
import com.linghu.utils.AnswerDoing;
import org.springframework.web.bind.annotation.*;
import com.aliyun.broadscope.bailian.sdk.models.CompletionsRequest;
import com.aliyun.broadscope.bailian.sdk.models.CompletionsResponse;

import java.util.Scanner;

/**
 * @author linghu
 * @date 2023/11/1 21:50
 * 服务端程序，负责向大模型发送http请求，接受resp响应
 */
@RestController()
@RequestMapping(value = "/lingHuApi")
public class QueryBack {

    private String accessKeyId = Constrant.ACCESSKEYID;
    private String accessKeySecret = Constrant.ACCESSKEYSECRET;
    private String agentKey = Constrant.AGENTKEY;
    private String appId=Constrant.APPID;

    @PostMapping("/mode")
    @ResponseBody
    public String sendHttp(@RequestBody String ques){
        AccessTokenClient accessTokenClient =
                new AccessTokenClient(accessKeyId, accessKeySecret, agentKey);
        String token = accessTokenClient.getToken();

        BaiLianConfig config = new BaiLianConfig()
                .setApiKey(token);

//        System.out.println("请输入问题？");
//        Scanner scanner = new Scanner(System.in);
//        String next = scanner.next();
//        String prompt = next;
        //这个配置只支持单个问题提问
        CompletionsRequest request = new CompletionsRequest()
                .setAppId(appId)
                .setPrompt(ques);

        ApplicationClient client = new ApplicationClient(config);
        CompletionsResponse response = client.completions(request);
        //这段是答案展示
        String answer = response.getData().getText();
        System.out.println("=====answer======");
        System.out.println(AnswerDoing.removeSupTag(answer));
        return AnswerDoing.removeSupTag(answer);
    }
}
